﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using TriviaViewModel.ServiceReference1;
using System.Windows.Input;
using System.Windows.Forms;
using System.IO;
using GalaSoft.MvvmLight.Command;

namespace TriviaViewModel
{
    public class AddQuestionViewModel : ViewModelBase
    {

        int numberOfCheckBoxesClicks;
        #region Properties
        /// <summary>
        /// question added to db
        /// </summary>
        private string _questionAdded;
        public string QuestionAdded
        {
            get { return _questionAdded; }
            set
            {
                if (_questionAdded != value)
                {
                    _questionAdded = value;
                    RaisePropertyChanged("QuestionAdded");
                }
            }
        }


        private ObservableCollection<string> _answerStrings;

        public ObservableCollection<string> AnswerStrings
        {
            get { return _answerStrings; }
            set
            {
                if (_answerStrings != value)
                {
                    _answerStrings = value;
                    RaisePropertyChanged("AnswerStrings");
                }
            }
        }


        private bool _isRightAnswer;
        public bool IsRightAnswer
        {
            get { return _isRightAnswer; }
            set
            {
                if (_isRightAnswer != value)
                {
                    _isRightAnswer = value;
                    RaisePropertyChanged("IsRightAnswer");
                }
            }
        }

        private BitmapSource _questionAddedImageSource;
        public BitmapSource QuestionAddedImageSource
        {
            get { return _questionAddedImageSource; }
            set
            {
                if (_questionAddedImageSource != value)
                {
                    _questionAddedImageSource = value;
                    RaisePropertyChanged("QuestionAddedImageSource");
                }
            }
        }


        /// <summary>
        /// difficulty for  question added
        /// </summary>
        private string _selectedDifficulty;
        public string SelectedDifficulty
        {
            get { return _selectedDifficulty; }
            set
            {
                if (_selectedDifficulty != value)
                {
                    _selectedDifficulty = value;
                    RaisePropertyChanged("SelectedDifficulty");
                }
            }
        }

        /// <summary>
        /// topic for question added
        /// </summary>
        private string _selectedTopic;
        public string SelectedTopic
        {
            get { return _selectedTopic; }
            set
            {
                if (_selectedTopic != value)
                {
                    _selectedTopic = value;
                    RaisePropertyChanged("SelectedTopic");
                }
            }
        }

        private ObservableCollection<AnswerCalss> _answers;
        public ObservableCollection<AnswerCalss> Answers
        {
            get { return _answers; }
            set
            {
                if (_answers != value)
                {
                    _answers = value;
                    RaisePropertyChanged("Answers");
                }
            }
        }
        private string _clue;
        public string Clue
        {
            get { return _clue; }
            set
            {
                if (_clue != value)
                {
                    _clue = value;
                    RaisePropertyChanged("Clue");
                }

            }
        }
        /// <summary>
        /// game topics
        /// </summary>
        private List<string> _topics;
        public List<string> Topics
        {
            get { return _topics; }
            set
            {
                if (_topics != value)
                {
                    _topics = value;
                    RaisePropertyChanged("Topics");
                }
            }
        }
        /// <summary>
        /// game difficulties
        /// </summary>
        private List<string> _difficulties;
        public List<string> Difficulties
        {
            get { return _difficulties; }
            set
            {
                if (_difficulties != value)
                {
                    _difficulties = value;
                    RaisePropertyChanged("Difficulties");
                }
            }
        }

        #endregion


        public ICommand AddQuestionDetailsCommand { get; set; }
        public ICommand AddAnswerCommand { get; set; }
        public ICommand CheckedCommand { get; set; }
        public ICommand AddPictureCommand { get; set; }


        public AddQuestionViewModel()
        {
            Answers = new ObservableCollection<AnswerCalss>();
            using (var proxy = new ServiceReference1.GameServiceClient())
            {
                Topics = proxy.GetTopics().ToList();
                Difficulties = proxy.GetDificulties().ToList();
            }

            AddQuestionDetailsCommand = new RelayCommand(() => AddQuestionCommandExecute());
            AddAnswerCommand = new RelayCommand(() => AddAnswerCommandExecute(),
                () =>
                {
                    return (AnswerStrings != null && AnswerStrings.Count == 4) ? true : false;
                });
            CheckedCommand = new RelayCommand(() => CheckedCommandExecute());
            AddPictureCommand = new RelayCommand(() => AddPictureCommandExecute());


        }



        private void AddQuestionCommandExecute()
        {
            if (SelectedDifficulty != null && SelectedTopic != null && QuestionAdded != null)
            {
                int qID;
                using (var proxy = new GameServiceClient())
                {

                    QuestionCalss question = new QuestionCalss()
                    {
                        DificultyId = proxy.GetDifficultyId(SelectedDifficulty),
                        TypeId = proxy.GetTypeId(SelectedTopic),
                        QuestionString = QuestionAdded,
                        Clue = (this.Clue != null) ? this.Clue : ""

                    };
                    qID = proxy.AddQuestion(question);

                    if (Answers != null)
                    {
                        foreach (var item in Answers)
                            item.QuestionId = qID;
                    }
                    proxy.AddAnswers(Answers.ToArray());
                }
                Answers.Clear();
                QuestionAdded = null;

                Clue = null;
                SelectedTopic = null;
                SelectedDifficulty = null;
            }
        }

        private void AddAnswerCommandExecute()
        {
            foreach (var item in AnswerStrings)
            {
                AnswerCalss answer = new AnswerCalss();
                answer.AnswerString = item;
                answer.IsRightAnswer = IsRightAnswer;
                Answers.Add(answer);
            }
                
            


        }

        private void CheckedCommandExecute()
        {
            if (numberOfCheckBoxesClicks % 2 == 0)
                IsRightAnswer = false;
            else
                IsRightAnswer = true;
        }

        private void AddPictureCommandExecute()
        {
            OpenFileDialog fileloader = new OpenFileDialog();
            fileloader.ShowDialog();
            fileloader.DefaultExt = ".png";

            using (FileStream fileStream = File.OpenRead(fileloader.FileName))
            {
                MemoryStream memStream = new MemoryStream();
                memStream.SetLength(fileStream.Length);
                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

                var img = new BitmapImage(new Uri(fileloader.FileName));
                // _questionAddedImageSource = new BitmapSource(new Uri(fileloader.FileName)));



            }
        }


    }
}
